#include "recboxitem.h"
#include "ui_recboxitem.h"

#include <QDebug>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //拦截鼠标进入和离开的事件，对musicImageBox安装事件过滤器
    ui->musicImageBox->installEventFilter(this);
}


RecBoxItem::~RecBoxItem()
{
    delete ui;
}


//事件拦截
bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    //如果光标进入musicImageBox控件内部
    if(watched == ui->musicImageBox)
    {
        if(QEvent::Enter == event->type())
        {
            //添加图片上移动的动画
            //qDebug() << "鼠标进入";

            QPropertyAnimation* animal = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animal->setDuration(100);
            animal->setStartValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animal->setEndValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animal->start();

           // delete  animal; 释放对象，当动画效果结束的时候会发送信号
            connect(animal, &QPropertyAnimation::finished, this, [=](){ /*qDebug() << "销毁成功1";*/ delete animal;});
        }
        else if(QEvent::Leave == event->type())
        {
            //添加图片下移动的动画
            //qDebug() << "鼠标离开";
            QPropertyAnimation* animal = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animal->setDuration(100);
            animal->setStartValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animal->setEndValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animal->start();

          //  delete  animal;
            connect(animal, &QPropertyAnimation::finished, this, [=](){ /*qDebug() << "销毁成功2";*/ delete animal;});
        }

        return true;
    }

    return QObject::eventFilter(watched, event); //如果不是鼠标进入和离开事件，则交给父控件去处理
}


//设置recBoxItemText 的 文本
void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

//设置那个图片
void RecBoxItem::setRecImage(const QString &imagePath)
{
    QString style = "background-image:url(" + imagePath+  ")";
    ui->recMusicImage->setStyleSheet(style);
}




